<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script type="text/javascript">
    let socket
    if (window.WebSocket) {
        // 创建一个websocket连接


        socket = new WebSocket("ws://localhost:8888/some")

        socket.onopen = function (ev) {
            let responseText = document.getElementById("responseText");
            responseText.value = "已建立连接。"

        }

        socket.onmessage = function (ev) {
            let responseText = document.getElementById("responseText")
            responseText.value = responseText.value + "\n" + ev.data
        }

        socket.onclose = function (ev) {
            let responseText = document.getElementById("responseText")
            responseText.value = "\n" + "连接关闭"
        }

    } else{
        alert("浏览器不支持websocket");
    }

    function send(msg) {
        if (!window.WebSocket) return
        if (socket.readyState = WebSocket.OPEN) {
            socket.send(msg);
        }
    }
</script>
<body>

    <form>
        <textarea id="message" style="width: 150px;height: 150px;"></textarea>
        <input type="button" value="发送" onclick="send(this.form.message.value)" />
        <textarea id="responseText" style="width: 150px;height: 150px;"></textarea>
    </form>
</body>
</html>